package com.example.lyl.yuejian.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.OrderListBean;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by dllo on 18/3/23.
 */

public class OrderLvAdapter extends BaseAdapter {

    OrderListBean orderListBean;
    BtnClickListener listener;

    public void setListener(BtnClickListener listener) {
        this.listener = listener;
    }

    public void setOrderListBean(OrderListBean orderListBean) {
        this.orderListBean = orderListBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (orderListBean != null) {
            return orderListBean.getData().size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_orderlist, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(parent.getContext()).load(
                "http://" + orderListBean.getData().get(position).getShop().getSpic()).into(holder.goodsIv);
        holder.itemNameTv.setText(orderListBean.getData().get(position).getShop().getSname());
        holder.itemPriceTv.setText(orderListBean.getData().get(position).getShop().getSprice()*orderListBean.getData().get(position).getCount()+"");
        if (orderListBean.getData().get(position).getStatus() == 1 ){
                holder.statusTv.setVisibility(View.VISIBLE);
                holder.zfBtn.setVisibility(View.GONE);
        }else {
            holder.statusTv.setVisibility(View.GONE);
            holder.zfBtn.setVisibility(View.VISIBLE);
            holder.zfBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  listener.btnClickLisener(orderListBean.getData().get(position).getOid());
                }
            });
        }
        return convertView;
    }



     class ViewHolder {
        @BindView(R.id.goods_iv)
        ImageView goodsIv;
        @BindView(R.id.item_name_tv)
        TextView itemNameTv;
        @BindView(R.id.item_price_tv)
        TextView itemPriceTv;
        @BindView(R.id.status_tv)
        TextView statusTv;
        @BindView(R.id.zf_btn)
        Button zfBtn;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface BtnClickListener{
        void btnClickLisener(int oid);
    }

}
